//
//  HNReleaseView.m
//  shezhan
//
//  Created by admin on 16/8/15.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNReleaseView.h"
#import "HNNativeHomeController.h"

static HNReleaseView * releaseView;

@implementation HNReleaseView{
    UIView * _contentView;
}

+(instancetype)shareInstance{
    if (!releaseView) {
        releaseView = [[HNReleaseView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
        [releaseView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4]];
        [releaseView createUI];
    }
    return releaseView;
}
-(void)createUI{
    for (UIView * view in _contentView.subviews) {
        [view removeFromSuperview];
    }
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, releaseView.frame.size.height - 30, mainScreenSize.width, 210)];
    UIImageView * backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainScreenSize.width, 210)];
    [backView setImage:[UIImage imageNamed:@"hu"]];
    [_contentView addSubview:backView];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(mainScreenSize.width/3, 160, mainScreenSize.width/3, 50)];
    [button setImage:[UIImage imageNamed:@"icon_cha"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(hiden) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:button];
    
    UILabel*titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, mainScreenSize.width, 16)];
    titleLabel.text = @"选择售卖方式";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:titleLabel];
    
    UILabel*otherTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainScreenSize.width/4, 40 , mainScreenSize.width/2, 13)];
    otherTitleLabel.text = @"查看区别";
    otherTitleLabel.textColor = RGB(155, 155, 155);
    otherTitleLabel.font = [UIFont systemFontOfSize:11];
    otherTitleLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:otherTitleLabel];
    
    UIButton * promptbutton = [[UIButton alloc]initWithFrame:CGRectMake(mainScreenSize.width/4, 20 , mainScreenSize.width/2, 40)];
    [promptbutton setTintColor:RGBA(255, 255, 255, 0)];
//    promptbutton.backgroundColor = [UIColor grayColor];
    [promptbutton setTitle: nil forState:UIControlStateNormal];
    [promptbutton setTitleColor:RGB(155, 155, 155) forState:UIControlStateNormal];
    promptbutton.tag = 2004;
    [promptbutton addTarget:self action:@selector(showDifferent:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:promptbutton];
    
    UIImageView * problemView = [[UIImageView alloc]initWithFrame:CGRectMake(mainScreenSize.width/2 + 30, otherTitleLabel.frame.origin.y,13, 13)];
    [problemView setImage:[UIImage imageNamed:@"problem.png"]];
    [_contentView addSubview:problemView];
    
//    NSArray * arr = [NSArray arrayWithObjects:@"portfolio",@"clock",@"camera", nil];
//    NSArray * arrTitle = [NSArray arrayWithObjects:@"寄卖",@"闲置",@"回收", nil];
       NSArray * arr = [NSArray arrayWithObjects:@"portfolio",@"clock", nil];
     NSArray * arrTitle = [NSArray arrayWithObjects:@"寄卖速卖",@"闲置代购", nil];
    NSArray *arrDetailMsg = [NSArray arrayWithObjects:@"平台代售或商品回收",@"个人发布售卖", nil];
    for (int i = 0; i<arr.count; i++) {
        UIButton * tabbarButton = [[UIButton alloc]initWithFrame:CGRectMake(mainScreenSize.width/arrTitle.count*i, 45, mainScreenSize.width/arrTitle.count, 100)];
        tabbarButton.tag = 20000+i;
        UIImage * image = [UIImage imageNamed:arr[i]];
        [tabbarButton setImage:image forState:UIControlStateNormal];

        [tabbarButton setImageEdgeInsets:UIEdgeInsetsMake(0, mainScreenSize.width/(2*arrTitle.count)-image.size.width/2, 20, mainScreenSize.width/(2*arrTitle.count)-image.size.width/2)];
        [tabbarButton addTarget:self action:@selector(tabButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [tabbarButton setTitle:arrTitle[i] forState:UIControlStateNormal];
        [tabbarButton setTitleColor:[AppConfig colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        [tabbarButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [tabbarButton setTitleEdgeInsets:UIEdgeInsetsMake(81, -image.size.width, 5, 0)];
        
        [tabbarButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        //[tabbarButton setBackgroundColor:[UIColor redColor]];
        [_contentView addSubview:tabbarButton];
        
        UILabel *detailMsgLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainScreenSize.width/arrDetailMsg.count*i, 140, mainScreenSize.width/arrDetailMsg.count, 30)];
        detailMsgLabel.font = [UIFont systemFontOfSize:12];
        detailMsgLabel.textColor = [AppConfig colorWithHexString:@"AAAAAA"];
        detailMsgLabel.text = arrDetailMsg[i];
        detailMsgLabel.textAlignment = NSTextAlignmentCenter;
        [_contentView addSubview:detailMsgLabel];
    }
    

    
    [releaseView addSubview:_contentView];
}

//查看不同
-(void)showDifferent:(UIButton *)btn{
    releaseView.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.1 animations:^{
        [_contentView setFrame:CGRectMake(0, releaseView.frame.size.height, mainScreenSize.width, 210)];
    } completion:^(BOOL finishid){
        [releaseView removeFromSuperview];
        [self.delegate HNReleaseView:self didSelectButton:btn];
    }];
}

-(void)tabButtonClick:(UIButton *)btn{
    releaseView.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.1 animations:^{
        [_contentView setFrame:CGRectMake(0, releaseView.frame.size.height, mainScreenSize.width, 210)];
    } completion:^(BOOL finishid){
        [releaseView removeFromSuperview];
        [self.delegate HNReleaseView:self didSelectButton:btn];
    }];
}

-(void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:releaseView];
    [UIView animateWithDuration:0.4 animations:^{
        [_contentView setFrame:CGRectMake(0, releaseView.frame.size.height-210, mainScreenSize.width, 210)];
    } completion:^(BOOL finishid){
        releaseView.userInteractionEnabled = YES;
    }];
}
-(void)hiden{
    releaseView.userInteractionEnabled = NO;
    [UIView animateWithDuration:0.4 animations:^{
        [_contentView setFrame:CGRectMake(0, releaseView.frame.size.height, mainScreenSize.width, 210)];
    } completion:^(BOOL finishid){
        [releaseView removeFromSuperview];
    }];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hiden];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
